<template>
	<view class="main-container">
		<!-- 顶部栏 -->
		<view class="header-section">
			<uni-icons type="back" size="30" @click="goBack"></uni-icons>
			<!-- <view class="flex justify-between align-center">
				<input type="text" class="search-input" placeholder="这里放搜索框" />
				<uni-icons type="search" size="30" class="search-icon"></uni-icons>
			</view> -->
		</view>

		<!-- 分类选择 -->
		<view class="categories">
			<view class="category-item" :class="{ 'active-category': selectedCategory === category }"
				v-for="category in categories" :key="category" @click="selectCategory(category)">
				{{ category }}
			</view>
		</view>

		<view class="content-container">
			<!-- 侧边栏(仅在宠物页面显示) -->
			<view v-if="selectedCategory === '宠物'" class="sidebar">
				<view class="sidebar-item" :class="{ 'active-sidebar': selectedPetType === petType }"
					v-for="petType in petTypes" :key="petType" @click="selectPetType(petType)">
					{{ petType }}
				</view>
			</view>

			<!-- 商品列表 -->
			<scroll-view class="product-list" scroll-y>
				<view class="product-action grid col-2">
					<view class="product-item" v-for="(product, index) in products" :key="index"
						@click="goToProductDetail(product)">
						<img v-if="product.petLogo || product.productLogo"
							:src="baseUrl + (product.petLogo || product.productLogo)" class="product-image"
							mode="scaleToFill"></img>
						<view class="product-info">
							<view v-if="selectedCategory === '宠物' ? product.petName : product.productName"
								class="product-name">
								{{ selectedCategory === '宠物' ? product.petName : product.productName }}
							</view>
							<view v-if="selectedCategory === '宠物' ? product.petPrice : product.productPrice"
								class="product-description">
								￥{{ selectedCategory === '宠物' ? product.petPrice : product.productPrice }}
							</view>
							<view v-if="selectedCategory === '宠物' ? product.storeId : product.productDescription"
								class="product-description">
								{{ selectedCategory === '宠物' ?  storeInfo(product.storeId).storeName  : product.productDescription }}
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import {
		getpetsBystatus,
		getpetsByPetType
	} from '@/api/pets.js'; // 导入宠物接口
	import {
		getProductList,
		getProductsByCategory
	} from '@/api/login.js';
	import config from '@/config';
	import {
		getAllShop
	} from '../../api/shop';
	const baseUrl = config.baseUrl;

	export default {
		data() {
			return {
				categories: ['宠物', '主食', '清洁', '玩具', '服饰'],
				petTypes: ['显示所有', '猫', '狗', '鸡', '鸟', '鱼', '蛇', '鼠', '猪'],
				products: [],
				selectedCategory: '',
				selectedPetType: '显示所有',
				baseUrl: baseUrl,
				selectedSort: '',
				stores: [],
				queryInfo: {
					storeId: '',
					selectedCategory: '',
				},
			};
		},
		onLoad(options) {
			if (options.category) {
				this.selectedCategory = options.category;
				this.fetchProducts(this.selectedCategory);
			}
			// 从 options 中获取传递的商店ID
			const storeId = options.storeId;
			console.log(options);
			if (storeId) {
				this.queryInfo.storeId = storeId;
				this.queryInfo.selectedCategory = "主食";
				this.selectedCategory = '主食';
			}
		},
		created() {
			this.getShop();
		},
		computed: {
			storeInfo() {
				return (storeId) => {
					const store = this.stores.find(s => s.storeId === storeId);
					return store ? store : {
						name: '未知商店',
						address: ''
					};
				};
			}
		},
		methods: {
			goBack() {
				uni.navigateBack();
			},
			getShop() {
				getAllShop().then(res => {
					this.stores = res.data;
				})
			},
			selectCategory(category) {
				this.selectedCategory = category;
				this.selectedPetType = '显示所有'; // 重置宠物分类为显示所有
				if(this.queryInfo.storeId ==''){
					this.fetchProducts(category);
				}
			},
			selectPetType(petType) {
				this.selectedPetType = petType;
				this.fetchPetsByType(petType);
			},
			fetchProducts(category) {
				if (category === '宠物') {
					this.fetchPetsByType(this.selectedPetType);
				} else {
					getProductsByCategory(category)
						.then((response) => {
							if (response.code === 200) {
								this.products = response.data;
								console.log('Fetched products:', this.products);
							}
						})
						.catch((error) => {
							console.error(error);
						});
				}
			},
			fetchPetsByType(petType) {
				if (petType === '显示所有') {
					getpetsBystatus()
						.then((response) => {
							if (response.code === 200) {
								this.products = response.data;
								console.log('Fetched all pets:', this.products);
							}
						})
						.catch((error) => {
							console.error(error);
						});
				} else {
					getpetsByPetType(petType)
						.then((response) => {
							if (response.code === 200) {
								this.products = response.data;
								console.log(`Fetched ${petType} pets:`, this.products);
							}
						})
						.catch((error) => {
							console.error(error);
						});
				}
			},
			getProductWithQuery(){
				getProductList(this.queryInfo).then(res=>{
					console.log(res);
					this.products = res.data.rows;
				})
			},
			goToProductDetail(product) {
				if (!product) {
					console.error('Product is undefined.');
					return;
				}

				let url = '';
				if (this.selectedCategory === '宠物') {
					if (product.petId) {
						url = `/pages/category/ProductDetail/PetDetail?petId=${product.petId}`;
					} else {
						console.error('宠物ID不存在');
						return;
					}
				} else {
					if (product.productId) {
						url = `/pages/category/ProductDetail/ProductDetail?productId=${product.productId}`;
					} else {
						console.error('产品ID不存在');
						return;
					}
				}

				uni.navigateTo({
					url: url,
				});
			},
		},
	};
</script>

<style scoped>
	.main-container {
		width: 100%;
		height: 100%;
		background-color: #f5f6f7;
		display: flex;
		flex-direction: column;
	}

	.header-section {
		padding: 15px;
		background-color: #ffcbb2;
		color: white;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.search-input {
		flex: 1;
		margin: 0 10px;
		padding: 5px;
		border-radius: 5px;
		border: none;
	}

	.search-icon {
		color: white;
	}

	.categories {
		display: flex;
		justify-content: space-around;
		padding: 10px;
		background-color: #fff;
	}

	.category-item {
		padding: 10px;
		font-size: 16px;
		color: #333;
		cursor: pointer;
	}

	.category-item.active-category {
		color: white;
		background-color: #ffcbb2;
		border-radius: 5px;
	}

	.content-container {
		display: flex;
		flex: 1;
		overflow: hidden;
		height: 100%;
	}

	.sidebar {
		width: 20%;
		padding: 10px;
		background-color: #fff;
		display: flex;
		flex-direction: column;
		border: #cecece solid;
		height: 100%;
	}

	.sidebar-item {
		padding: 10px;
		font-size: 16px;
		color: #333;
		cursor: pointer;
		border: 1px #fff7c4 solid;
	}

	.sidebar-item.active-sidebar {
		color: white;
		background-color: #ffcbb2;
		border-radius: 5px;
	}

	.product-list {
		flex: 1;
		padding: 10px;
		background-color: #fff;
		overflow-y: auto;
	}

	.product-item {
		display: flex;
		flex-direction: column;
		background-color: white;
		border-radius: 5px;
		overflow: hidden;
		cursor: pointer;
	}

	.product-image {
		width: 100%;
		height: auto;
		object-fit: cover;
	}

	.product-info {
		padding: 10px;
	}

	.product-name {
		font-size: 18px;
		font-weight: bold;
	}

	.product-description {
		font-size: 14px;
		color: #666;
	}
</style>